/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.sd.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.sd.common.util.DateUtils;
import com.jeesite.modules.sd.entity.DocDeliverRecord;
import com.jeesite.modules.sd.entity.DocRecv;
import com.jeesite.modules.sd.service.DocDeliverRecordService;
import com.jeesite.modules.sd.service.DocRecvService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sd.entity.DocDestoyRecord;
import com.jeesite.modules.sd.service.DocDestoyRecordService;

import java.util.Date;
import java.util.List;

/**
 * sd_doc_destoy_recordController
 * @author ysj
 * @version 2020-08-10
 */
@Controller
@RequestMapping(value = "${adminPath}/sd/docDestoyRecord")
public class DocDestoyRecordController extends BaseController {

	@Autowired
	private DocDestoyRecordService docDestoyRecordService;

	@Autowired
	private DocRecvService recvService;

	@Autowired
	private DocDeliverRecordService recordService;

	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DocDestoyRecord get(String id, boolean isNewRecord) {
		return docDestoyRecordService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("sd:docDestoyRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(DocDestoyRecord docDestoyRecord, Model model) {
		model.addAttribute("docDestoyRecord", docDestoyRecord);
		return "modules/sd/docDestoyRecordList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("sd:docDestoyRecord:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DocDestoyRecord> listData(DocDestoyRecord docDestoyRecord, HttpServletRequest request, HttpServletResponse response) {
		docDestoyRecord.setPage(new Page<>(request, response));
		Page<DocDestoyRecord> page = docDestoyRecordService.findPage(docDestoyRecord);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("sd:docDestoyRecord:view")
	@RequestMapping(value = "form")
	public String form(DocDestoyRecord docDestoyRecord, Model model) {
		model.addAttribute("docDestoyRecord", docDestoyRecord);
		return "modules/sd/docDestoyRecordForm";
	}

	/**
	 *查询销毁记录 并跳转到记录页面
	 * @return
	 */
	@RequestMapping(value = "getList")
	public String getList(String  id,Model model) {
		DocDestoyRecord destoyRecord=new DocDestoyRecord();
		destoyRecord.setDestoyId(id);
		List<DocDestoyRecord> list = docDestoyRecordService.findList(destoyRecord);
		model.addAttribute("id",id);
        model.addAttribute("docDestoyRecord",list);
		return "modules/sd/docDestoyRecordsDetails";
	}

	/**
	 * 查询销毁记录
	 * @param destoyRecord
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getList1")
	@ResponseBody
	public List getList1(DocDestoyRecord destoyRecord,Model model,String id) {

		destoyRecord.setDestoyId(id);
		List<DocDestoyRecord> list=docDestoyRecordService.getDestoy(id);
	    if (CollectionUtils.isNotEmpty(list)){
	    	list.forEach(li->{
	    		String recvId=li.getRecvId();
	    		String deliverId=li.getDeliverId();
				DocRecv docRecv = recvService.get(recvId);
				DocDeliverRecord docDeliverRecord = recordService.get(deliverId);
				li.setTitle(docRecv.getDocTitle());
				li.setDocNum(docRecv.getDocFullNum());
				li.setDoCode(docRecv.getDocSysCode());
				li.setDocCopyNo(docDeliverRecord.getDocCopyNo().toString());


			});
		}

		return list;
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("sd:docDestoyRecord:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DocDestoyRecord docDestoyRecord) {
		docDestoyRecordService.save(docDestoyRecord);
		return renderResult(Global.TRUE, text("保存sd_doc_destoy_record成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("sd:docDestoyRecord:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DocDestoyRecord docDestoyRecord) {
		docDestoyRecordService.delete(docDestoyRecord);
		return renderResult(Global.TRUE, text("删除sd_doc_destoy_record成功！"));
	}
	
}